package com.yzg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yzg.utils.R;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.yzg.mapper.ProductCategoryMapper;
import com.yzg.pojo.ProductCategory;
import com.yzg.service.ProductCategoryService;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
* @Author: decade
* @Description: ${description}
* @Date: 2021/1/9 17:49
**/
@Service
public class ProductCategoryServiceImpl implements ProductCategoryService{

    @Resource
    private ProductCategoryMapper productCategoryMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return productCategoryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(ProductCategory record) {
        return productCategoryMapper.insert(record);
    }

    @Override
    public int insertSelective(ProductCategory record) {
        return productCategoryMapper.insertSelective(record);
    }

    @Override
    public ProductCategory selectByPrimaryKey(Long id) {
        return productCategoryMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(ProductCategory record) {
        return productCategoryMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(ProductCategory record) {
        return productCategoryMapper.updateByPrimaryKey(record);
    }

    //查询商品分类树形列表
    @Override
    public List<ProductCategory> pageTreeList() {
        QueryWrapper<ProductCategory> wrapper = new QueryWrapper<>();
        productCategoryMapper.pageList();
        return productCategoryMapper.pageList();
    }

    //修改导航栏状态
    @Override
    public int updateNavStatus(List<Long> ids, Integer navStatus) {
        //查询该id下是否有子id
        QueryWrapper<ProductCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",ids.get(0));
       List<ProductCategory> list = productCategoryMapper.selectList(wrapper);

       if(list!=null||list.size()!=0){
            list.forEach(productCategory -> {
                if(productCategory.getNavStatus()!=navStatus){
                    ids.add(productCategory.getId());
                }
            });
       }
        ids.forEach(aLong -> System.out.println(aLong));
        for (Long id : ids){
            ProductCategory productCategory = new ProductCategory();
            productCategory.setNavStatus(navStatus);
            productCategory.setId(id);
            int row = productCategoryMapper.updateById(productCategory);
            if(row<=0){
                return 0;
            }
        }
        return 1;
    }

    //修改显示状态
    @Override
    public int updateShowStatus(List<Long> ids, Integer showStatus) {
        //查询该id下是否有子id
        QueryWrapper<ProductCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",ids.get(0));
        List<ProductCategory> list = productCategoryMapper.selectList(wrapper);

        if(list!=null&&list.size()!=0){
            list.forEach(productCategory -> {
                if(productCategory.getShowStatus()!=showStatus){
                    ids.add(productCategory.getId());
                }
            });
        }
        ids.forEach(aLong -> System.out.println(aLong));
        for (Long id : ids){
            ProductCategory productCategory = new ProductCategory();
            productCategory.setShowStatus(showStatus);
            productCategory.setId(id);
            int row = productCategoryMapper.updateById(productCategory);
            if(row<=0){
                return 0;
            }
        }
        return 1;
    }

    //根据id获取商品分类
    @Override
    public ProductCategory getProductCateById(Long id) {

        return productCategoryMapper.selectById(id);
    }

    //修改商品分类
    @Override
    public int updateProductCate(ProductCategory productCate) {

        //修改商品分类属性
        //先删除再修改

        //修改商品分类
        return productCategoryMapper.updateById(productCate);
    }

    //删除商品分类
    @Override
    public int deleteById(Long id) {
        //先检查是否有子类，如果有先删除子类，再删除父
        QueryWrapper<ProductCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        List<ProductCategory> list = productCategoryMapper.selectList(wrapper);
        List<Long> ids = new ArrayList<>();
        if(list!=null&&list.size()!=0){
            list.forEach(productCategory -> {
                ids.add(productCategory.getId());
            });
        }
        ids.add(id);
        return productCategoryMapper.deleteBatchIds(ids);
    }

    ////添加商品分类
    @Override
    public int add(ProductCategory productCate) {
        if(productCate.getParentId()==0){
            productCate.setLevel(0);
        }else {
            productCate.setLevel(1);
        }
        return productCategoryMapper.insert(productCate);
    }
}
